package edu.ucsd.cs110.mycity.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
import android.util.Log;
import edu.ucsd.cs110.mycity.debug.CoordinatePrintout;
import edu.ucsd.cs110.mycity.services.ChatService;

public final class XMPPController {
	
	private static XMPPConnection connection;
	private static Roster roster;
	private static Collection<String> buddies;
	private static ArrayList<HashMap<String, String>> rosterMap = new ArrayList<HashMap<String,String>>();
	private static ChatService chatService;
	
	public static void connectToGtalk(final String username, final String password) {
		if (isLoggedIn()) {
			return;
		}
		
		Thread t = new Thread(new Runnable() {

			@Override
			public void run() {
				ConnectionConfiguration connConfig = new ConnectionConfiguration(
						"talk.google.com", 5222, "gmail.com");
				XMPPConnection connection = new XMPPConnection(connConfig);
				try {
					connection.connect();
					Log.i("XMPP - Connection", "Connected to " + connection.getHost());
				} catch (XMPPException e) {
					Log.e("XMPP - Connection", "Failed to connect to  Gtalk server.");
				}
				try {
					connection.login(username, password);
					Log.i("XMPP - Logging in", "Logged in as " + connection.getUser());
					setConnection(connection);
					ViewController.doLoginIntent(); // Map Screen will initiate ChatService
					CoordinateController.addCoordinateListener(new CoordinatePrintout());
					setRoster(connection.getRoster());
					refreshBuddies();
				} catch (XMPPException e) {
					Log.e("XMPP - Logging in", "Failed to log in as " + username + ".");
				}
			}
		});
		t.start();
	}

	public static void refreshBuddies() {
		Collection<RosterEntry> re = roster.getEntries();
		ArrayList<String> buddies = new ArrayList<String>();
		ArrayList<HashMap<String,String>> rosterList = new ArrayList<HashMap<String,String>>();
		Iterator<RosterEntry> iter = re.iterator();
		while(iter.hasNext()) {
			RosterEntry e = iter.next();
			HashMap<String,String> map = new HashMap<String,String>();
			map.put("username", e.getUser());
			map.put("presence", roster.getPresence(e.getUser()).toString());
			rosterList.add(map);
			buddies.add(e.getUser());
		}
		setRosterMap(rosterList);
		setBuddies(buddies);
	}
	
	public static Collection<String> getBuddies() {
		return buddies;
	}
	
	public static void setBuddies(Collection<String> buddies) {
		System.out.println("Adding " + buddies.size() + " buddies");
		XMPPController.buddies = buddies;
	}
	
	public static boolean isLoggedIn() {
		return getConnection() != null;
	}
	
	public static void setPresence(Presence presence) {
		connection.sendPacket(presence);
	}

	public static void setConnection(XMPPConnection connection) {
		XMPPController.connection = connection;
	}


	public static XMPPConnection getConnection() {
		return connection;
	}

	public static void setRoster(Roster roster) {
		XMPPController.roster = roster;
	}

	public static Roster getRoster() {
		return roster;
	}

	public static ChatService getChatService() {
		return chatService;
	}

	public static void setChatService(ChatService chatService) {
		XMPPController.chatService = chatService;
	}

	public static ArrayList<HashMap<String, String>> getRosterMap() {
		return rosterMap;
	}

	public static void setRosterMap(ArrayList<HashMap<String, String>> rosterMap) {
		XMPPController.rosterMap = rosterMap;
	}

}